package org.lilred.nzbberry.download;

import java.util.*;
public class BlockingQueue {
	/**
    It makes logical sense to use a linked list for a FIFO queue,
    although an ArrayList is usually more efficient for a short
    queue (on most VMs).
	 */
	private final Vector queue = new Vector();
	/**
    This method pushes an object onto the end of the queue, and
    then notifies one of the waiting threads.
	 */
	public void push(Object o) {
		synchronized(queue) {
			queue.addElement(o);
			queue.notify();
		}
	}
	/**
    The pop operation blocks until either an object is returned
    or the thread is interrupted, in which case it throws an
    InterruptedException.
	 */
	public Object pop() throws InterruptedException {
		synchronized(queue) {
			while (queue.isEmpty()) {
				queue.wait();
			}
			Object returnValue = queue.firstElement();
			queue.removeElement(returnValue);
			return returnValue;
		}
	}
	
	/** Return the number of elements currently in the queue. */
	public int size() {
		return queue.size();
	}
}